const {
  WorkOrderMyIndexApi
} = require('../../../../common/apis/workOrder')
const mixin_formItemType = require('../../../../common/mixins/formItemType')
const {
  WxValidate
} = require('../../../../utils/WxValidate')

Component({
  behaviors: [mixin_formItemType],

  /**
   * 组件的属性列表
   */
  properties: {
    orderNum: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    remark: '',
    processEnclosure: [],
    rules: {
      remark: {
        required: true,
      },
    },
    message: {
      remark: {
        required: '请输入处理内容'
      }
    }
  },

  /**
   * 组件的方法列表
   */
  methods: {
    submit(callback) {

      const {
        remark,
        processEnclosure
      } = this.data
      const params = {
        orderNum: this.data.orderNum,
        remark,
        processEnclosure
      }
      const validate = new WxValidate(this.data.rules, this.data.message)

      if (!validate.checkForm(params)) {
        const error = validate.errorList[0]
        wx.showToast({
          title: error.msg,
          icon: 'error'
        })
        return false
      }
      WorkOrderMyIndexApi.handleWorkOrder(params, ({
        code,
        msg
      }) => {
        if (code === 200) {
          wx.showToast({
            title: '处理成功',
            icon: "success"
          })
          wx.$_.delay(callback, 1000)

        } else {
          wx.showToast({
            title: msg,
            icon: 'error'
          })
        }
      })
    },
    //  监听附件的值
    onUploadData({
      detail
    }) {
      const {
        value,
        type
      } = detail
      if (type === 'fileList') {
        this.setData({
          processEnclosure: value
        })
      } else {
        let {
          processEnclosure
        } = this.data
        processEnclosure.push(...value)
        this.setData({
          processEnclosure: processEnclosure
        })
      }

    },
  }
})